package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.util.AjaxResult;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotion.UserParm;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyEsService strategyEsService;

    @RequestMapping("")
    public String index(Model model){
//        commends
        List<StrategyCommend> commends = strategyCommendService.getTop5();
        model.addAttribute("commends",commends);

        //hotCds
        List<StrategyStatisVO> hotCds = strategyStatisVOService.queryHotTop10();
        model.addAttribute("hotCds", hotCds);

        //abroadCds
        List<StrategyStatisVO> abroadCds = strategyStatisVOService.queryAbroadSortTop10();
        model.addAttribute("abroadCds", abroadCds);

        //unabroadCds
        List<StrategyStatisVO> unabroadCds = strategyStatisVOService.queryChinaSortTop10();
        model.addAttribute("unabroadCds", unabroadCds);

        //themeCds
        List<Map<String, Object>> data = strategyEsService.queryThemeCommend();
        model.addAttribute("themeCds", data);

        //chinas
        List<StatisVO> chinas = strategyEsService.queryConditionChina();
        model.addAttribute("chinas", chinas);
        //abroads
        List<StatisVO> abroads = strategyEsService.queryConditionAbroad();
        model.addAttribute("abroads", abroads);
        //themes
        List<StatisVO> themes = strategyEsService.queryConditionThemes();
        model.addAttribute("themes", themes);
        return "/strategy/index";
    }


    @RequestMapping("detail")
    public String detail(Long id, Model model){
        //detail
        StrategyDetail strategyDetail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContentById(id);
        strategyDetail.setStrategyContent(content);
        model.addAttribute("detail",strategyDetail);
        //查询阅读数
        strategyStatisVOService.increaseViewNum(id,1);
        //共享vo对象
        StrategyStatisVO vo = strategyStatisVOService.getstrategyStatisVo(id);
        model.addAttribute("vo",vo);

        //热门攻略排行，阅读的加分
        strategyStatisVOService.addScoreHot(id, 1);
        return "/strategy/detail";
    }

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){

        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //dest
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        //标签
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags", tags);
        //分页
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParm UserInfo user){
        //评论需要显示出来的数据，详情参考笔记或者马蜂窝
        comment.setUsername(user.getNickname());
        comment.setHeadUrl(user.getHeadImgUrl());
        comment.setLevel(user.getLevel());
        comment.setCity(user.getCity());
        comment.setUserId(user.getId());
        strategyCommentService.save(comment);
        //评论数+1
        strategyStatisVOService.increaseReplyNum(comment.getDetailId(),1);
        StrategyStatisVO vo = strategyStatisVOService.getstrategyStatisVo(comment.getDetailId());

        //热门攻略排行，评论加分
        strategyStatisVOService.addScoreHot(comment.getDetailId(), 1);
        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo){
        //前端传过来的page分页
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "/strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, @UserParm UserInfo user){
        strategyCommentService.commentThumbUp(toid,user.getId());

        return AjaxResult.SUCCESS;
    }

    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid, @UserParm UserInfo user){
        if (user == null){
            //没登陆
            AjaxResult result = new AjaxResult(false, "请先进行登陆");
            result.setCode(102);//未登录操作
            return result;
        }

        //取消收藏和收藏
        boolean ret = strategyStatisVOService.favor(sid, user.getId());
        StrategyStatisVO vo = strategyStatisVOService.getstrategyStatisVo(sid);

        //国内排行，收藏的加分
        strategyStatisVOService.addScoreAbroadAndChina(sid, ret ? 1 : -1);

        return new AjaxResult(ret, "").addData(vo);
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParm UserInfo user){
        if (user == null){
            //没登陆
            AjaxResult result = new AjaxResult(false, "请先进行登陆");
            result.setCode(102);//未登录操作
            return result;
        }
        //今天已顶过或者没顶
        boolean ret = strategyStatisVOService.thumbup(sid, user.getId());
        StrategyStatisVO vo = strategyStatisVOService.getstrategyStatisVo(sid);

        //点赞：国内外排行加分
        if (ret){
            strategyStatisVOService.addScoreAbroadAndChina(sid, 1);
        }
        return new AjaxResult(ret, "").addData(vo);
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo){
        Page page = strategyEsService.query(qo);
        model.addAttribute("page", page);
        return "/strategy/searchPageTpl";
    }

}
